package com.learn.demo;

import com.learn.demo.entity.Ctest;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import java.util.List;

/**
 * 测试类
 *
 * @author banjiawei
 * @date 2021/04/07
 */
public class CtestDemo {
    public static void main(String[] args) {
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure() // configures settings from hibernate.cfg.xml
                .build();
        try {
            SessionFactory sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            List<Ctest> ctestList = session.createQuery("from CtestAlias", Ctest.class).list();
            for (Ctest ctest : ctestList) {
                System.out.println(ctest);
            }
            session.getTransaction().commit();
            session.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            StandardServiceRegistryBuilder.destroy( registry );
        }
    }

}
